/**
 * [一句话描述该类的功能]
 *
 * @author : [kai]
 * @version : [v1.0]
 * @createTime : [2022/10/20 19:43]
 */


package com.find.service.lmpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.find.dao.UserDao;
import com.find.domain.User;
import com.find.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> lqw = new QueryWrapper<User>();
        lqw.eq("user_name",username);
        User user = userDao.selectOne(lqw);
        if(user.getPassword().equals(password)){
            userDao.login(username);
            return user;
        }
        return null;
    }

    @Override
    public boolean logout(String username) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(User::getName,username);
        User user = userDao.selectOne(lqw);
        if(user!=null){
            userDao.logout(user.getId());
            return true;
        }
        return false;
    }

    @Override
    public boolean saveUser(User user) {
        return userDao.insert(user) > 0;
    }

    @Override
    public boolean findUser(String name){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(User::getName,name);
        int flag = userDao.selectCount(lqw);
        return flag>0;
    }

    @Override
    public boolean modify(User user) {
        return userDao.updateById(user) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return userDao.deleteById(id) > 0;
    }

    @Override
    public IPage<User> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        userDao.selectPage(page,null);
        return page;
    }

    @Override
    public String updateByName(Map<String,Object> user, String name) {
        UpdateWrapper<User> uw=new UpdateWrapper<User>();
        uw.eq("user_name",name);
        uw.set(user.get("newname")!=null,"user_name",user.get("newname"));
        uw.set(user.get("password")!=null,"user_password",user.get("password"));
        uw.set(user.get("sex")!=null,"user_sex",user.get("sex"));
        uw.set(user.get("tel")!=null,"user_tel",user.get("tel"));
        uw.set(user.get("comment")!=null,"user_comment",user.get("comment"));
        int flag=userDao.update(null,uw);
        if(flag!=0) {
            if(user.get("newname")!=null) {
                return user.get("newname").toString();
            }
            else{
                return name;
            }
        } else {
            return null;
        }
    }

    @Override
    public boolean logInStat(String name){
        QueryWrapper<User> qw=new QueryWrapper<User>();
        qw.eq("user_name",name);
        User user = userDao.selectOne(qw);
        if(user==null) {
            return false;
        }
        return user.getLogin()==1;
    }

    @Override
    public User getByName(String name){
        QueryWrapper<User> qw=new QueryWrapper<User>();
        qw.eq("user_name",name);
        return userDao.selectOne(qw);
    }
}
